﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using System.Data;

public partial class Admin_JiFen_JFList : System.Web.UI.Page
{
    private DataTable dc = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) {
            methods.SYypeBind(this.ddlState, null);
            grdbind();
        }
    }


    public void grdbind()
    {
        using (Entities bll = new Entities())
        {

            var Bpl = from j in bll.U_CZ.Where(j => j.CZstat == 1).OrderByDescending(j => j.CZID) select j;

            if (txtUtel.Text.Trim() != "")
            {

                var user = bll.User.SingleOrDefault(p => p.UTrueName == txtUtel.Text.Trim() || p.UNickName == txtUtel.Text.Trim() || p.UTel == txtUtel.Text.Trim());
                if (user != null)
                {
                    Bpl = Bpl.Where(p => p.UGuid == user.UGuid);
                }
            }

            if (ddlState.SelectedValue != "请选择")
            {
                Bpl = Bpl.Where(p => p.CZPayType == ddlState.SelectedValue);
            }

            //时间查询
            //业务逻辑：如果选择了两个时间，则按时间段查询，否则按单个时间查询
            if (!string.IsNullOrEmpty(txtStartTime.Text.Trim()) && !string.IsNullOrEmpty(txtEndtime.Text.Trim()))//按时间段查询
            {
                DateTime starttime = Convert.ToDateTime(txtStartTime.Text.Trim());
                DateTime endtime = Convert.ToDateTime(txtEndtime.Text.Trim());
                Bpl = Bpl.Where(p => p.CZTime > starttime && p.CZTime <= endtime);
            }
            else //根据单个时间查询
            {
                if (!string.IsNullOrEmpty(txtStartTime.Text.Trim()))
                {
                    DateTime startime = Convert.ToDateTime(txtStartTime.Text.Trim());
                    Bpl = Bpl.Where(p => p.CZTime == startime);
                }
                else if ((!string.IsNullOrEmpty(txtEndtime.Text.Trim())))
                {
                    DateTime endtime = Convert.ToDateTime(txtEndtime.Text.Trim());
                    Bpl = Bpl.Where(p => p.CZTime == endtime);
                }
            }

            AspNetPager1.RecordCount = Bpl.Count();
            this.rptList.DataSource = Bpl.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize).ToList();  //这里好像一定要Tolist();不然会有点错误；;
            this.rptList.DataBind();
        }
    }

    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {   //当页面页数改变时，执行这个方法；
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        grdbind();
    }

    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
        Entities bll = new Entities();
        U_CZ model = bll.U_CZ.First(p => p.CZID == id);
        switch (e.CommandName.ToLower())
        {
            case "del":
                bll.U_CZ.Remove(model);
                break;
        }
        bll.SaveChanges();
        Response.Redirect("SYList.aspx");

    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        grdbind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        grdbind();
    }


    public string getUser(string xx)
    {
        string Result ="系统" ;
        Entities db = new Entities();
        var _User = db.User.SingleOrDefault(p => p.UOpenID == xx);
        if (_User != null)
        { 
            Result = "微信昵称：" + _User.UNickName + "；姓名：" + _User.UTrueName + "；电话：" + _User.UTel + "；星级(" + _User.Ujifen+")";
        }
        return Result;

    }



}